import 'package:flutter/material.dart';
import 'package:flutter_android_multi_env/models/package_info_model.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({
    super.key,
    required this.title,
  });

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  String _appName = '暂无 app name';
  String _appEnv = '暂无 app env';
  String _appBuildName = '暂无 app build name';
  String _appBuildNumber = '暂无 app build number';
  PackageInfoModel _infoModel = PackageInfoModel();

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  void _showConfigInfos() async {
    if (const bool.hasEnvironment('APP_NAME')) {
      _appName = const String.fromEnvironment('APP_NAME');
    }

    if (const bool.hasEnvironment('APP_ENV')) {
      _appEnv = const String.fromEnvironment('APP_ENV');
    }

    if (const bool.hasEnvironment('APP_BUILD_NAME')) {
      _appBuildName = const String.fromEnvironment('APP_BUILD_NAME');
    }

    if (const bool.hasEnvironment('APP_BUILD_NUMBER')) {
      _appBuildNumber = const String.fromEnvironment('APP_BUILD_NUMBER');
    }

    _infoModel = await PackageInfoModel.getPackageInfo();

    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headlineMedium,
            ),

            /// 获取环境配置信息
            ElevatedButton(
              onPressed: () => _showConfigInfos(),
              child: const Text(
                '获取环境配置',
                style: TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: 26,
                ),
              ),
            ),
            Text('应用名称：$_appName'),
            Text('应用环境：$_appEnv'),
            Text('应用version：$_appBuildName'),
            Text('应用code：$_appBuildNumber'),

            /// packageInfo应用中获取信息
            Text('packageInfo获取应用名称：${_infoModel.appName ?? '暂无 app name'}'),
            Text('packageInfo获取应用version：${_infoModel.version ?? '暂无 version'}'),
            Text('packageInfo获取应用code：${_infoModel.buildNumber ?? '暂无 app code'}'),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}
